package net.neptech.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import net.neptech.enums.workflow.InstanceConstant;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;

/**
 * @Author Saya
 * @Date 2025/8/24 18:06
 * @Description TODO
 **/

@Slf4j
public class StarterUserTaskCreateListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        log.debug(delegateTask.getClass().getCanonicalName());
        TaskService taskService = SpringUtil.getBean(TaskService.class);

        String assignee = delegateTask.getAssignee();
        String name = delegateTask.getName();
        log.debug("任务{}-执行人:{}", name, assignee);
        String processInstanceId = delegateTask.getProcessInstanceId();
        TaskEntityImpl taskEntity = (TaskEntityImpl) delegateTask;
        String nodeId = taskEntity.getTaskDefinitionKey();
        String processDefinitionId = taskEntity.getProcessDefinitionId();
        //流程id
        RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);

        if (StrUtil.isBlank(assignee)) {

            runtimeService.setVariableLocal(taskEntity.getExecutionId(),
                    InstanceConstant.VariableKey.AUTO_COMPLETE_TASK, true);
            taskService.complete(taskEntity.getId());


        }

    }
}
